package com.neusoft.productsys.controller;

import com.neusoft.productsys.common.BeanTools;
import com.neusoft.productsys.entity.Product;
import com.neusoft.productsys.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/modify_product.do")
public class ModifyServflet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        ProductService productService = new ProductService();
        String method = req.getParameter("method");
        if(method.equals("del")) {
            String id = req.getParameter("pid");
            int flag = productService.del_Product(Integer.parseInt(id));
        }
        else if(method.equals("add"))
        {
            Product product = BeanTools.requestToBean(req,Product.class);
            int flag = productService.add_Product(product);
            //resp.sendRedirect("/showp.do");
        }
        else if(method.equals("update")){
            Product product = BeanTools.requestToBean(req,Product.class);
            int flag = productService.modify_Product(product);
        }

        req.getRequestDispatcher("showp.do").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
